/*
 * aatracker.c
 *
 *  Created on: 12/06/2009
 *      Author: andre
 */

#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>

#include "factories/socketFactory.h"
#include "aatracker.h"
#include "requesthandler.h"
#include "trackerlogger.h"


#include "tests/tests.h"

/*main source code file of the tracker*/

int main(int argc, char **argv) {

    //runSocketFactoryTests();

    //runUrlHandlerTestSuite();

    //runRequestHandlerTestSuite();

    runBencoderTestSuite();



    logInfo("initializing aatracker");
    logInfo("creating server socket");

   //criando server socket pro tracker. esse eh o socket que vai escutar a rede por conexoes
   int sock = newTCPServerSocket(TRACKER_PORT);

   if (sock <0){
       logStackTrace("main()",__FILE__, __LINE__);
       exit(-1);
   }

   logInfo("server socket created, waiting for requests");

   //struct sockaddr_in sockAddr;
   //socklen_t sockLen = sizeof(struct sockaddr_in);
   //int peerSock = accept(sock, (struct sockaddr*) &sockAddr, &sockLen);

  //recebendo o request num socket
   while (1){
       int peerSock = accept(sock, NULL, NULL);

       logDebugMessage("main()", "request received and accepted, initializing its handling", __FILE__, __LINE__, 0);
       //tratando o request
       handleRequest(peerSock);
   }

   return 0;
}
